package com.hkzhao.reportingsystem.config;

import com.hkzhao.reportingsystem.dto.QueueEnum;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//消息队列配置
@Configuration
public class RabbitMqConfig {
    //超时触发自动取消注册队列所绑定的交换机
    @Bean
    DirectExchange overTimeDirect(){
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_REGISTER_CANCLE.getExchange())
                .durable(true)
                .build();
    }
    //超时触发自动取消注册队列延迟队列所绑定的交换机
    @Bean
    DirectExchange overTimeTtlDirect()
    {
        return  (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_REGISTER_CANCLE_TTL.getExchange())
                .durable(true)
                .build();
    }
    //超时触发取消注册队列
    @Bean
    public Queue overTimeQueue()
    {
        return new Queue(QueueEnum.QUEUE_REGISTER_CANCLE.getName());
    }
    //超时触发取消注册延迟队列
    @Bean
    public Queue overTimeTtlQueue()
    {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_REGISTER_CANCLE_TTL.getName())
                .withArgument("x-dead-letter-exchange",QueueEnum.QUEUE_REGISTER_CANCLE.getExchange())
                //到期后转发到的交换机
                .withArgument("x-dead-letter-routing-key",QueueEnum.QUEUE_REGISTER_CANCLE.getRouteKey())
                //到期后转发的路由键
                .build();
    }
    //超时触发取消注册延迟队列绑定到交换机
    @Bean
    Binding overTimeTtlBinding(DirectExchange overTimeTtlDirect,Queue overTimeTtlQueue)
    {
        return BindingBuilder
                .bind(overTimeTtlQueue)
                .to(overTimeTtlDirect)
                .with(QueueEnum.QUEUE_REGISTER_CANCLE_TTL.getRouteKey());
    }
    //超时触发取消注册队列绑定到交换机
    @Bean
    Binding overTimeBinding(DirectExchange overTimeDirect,Queue overTimeQueue)
    {
        return BindingBuilder
                .bind(overTimeQueue)
                .to(overTimeDirect)
                .with(QueueEnum.QUEUE_REGISTER_CANCLE.getRouteKey());
    }
    @Bean
    //审核陈坤实际消息队列绑定的交换机
    DirectExchange verifySuccessDirect()
    {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_REGISTER_SUCCCE.getExchange())
                .durable(true)
                .build();
    }
    //审核陈坤队列延迟队列所绑定的交换机
    @Bean
    DirectExchange verifySuccessTtlDirect()
    {
        return  (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_REGISTER_SUCCESS_TTL.getExchange())
                .durable(true)
                .build();
    }
    //审核陈坤后的操作消息队列
    @Bean
    public Queue verifySuccessQueue()
    {
        return new Queue(QueueEnum.QUEUE_REGISTER_SUCCCE.getName());
    }
    //审核陈坤延迟队列
    @Bean
    public Queue verifySuccessTtlQueue()
    {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_REGISTER_SUCCESS_TTL.getName())
                .withArgument("x-dead-letter-exchange",QueueEnum.QUEUE_REGISTER_SUCCCE.getExchange())
                //到期后转发到的交换机
                .withArgument("x-dead-letter-routing-key",QueueEnum.QUEUE_REGISTER_SUCCCE.getRouteKey())
                //到期后转发的路由键
                .build();
    }
    //审核陈坤延迟队列绑定到交换机
    @Bean
    Binding verifySuccessTtlBinding(DirectExchange verifySuccessTtlDirect,Queue verifySuccessTtlQueue)
    {
        return BindingBuilder
                .bind(verifySuccessTtlQueue)
                .to(verifySuccessTtlDirect)
                .with(QueueEnum.QUEUE_REGISTER_CANCLE_TTL.getRouteKey());
    }
    //审核陈坤队列绑定到交换机
    @Bean
    Binding verifySuccessBinding(DirectExchange verifySuccessDirect,Queue verifySuccessQueue)
    {
        return BindingBuilder
                .bind(verifySuccessQueue)
                .to(verifySuccessDirect)
                .with(QueueEnum.QUEUE_REGISTER_CANCLE.getRouteKey());
    }
    //审核失败实际队列绑定交换机
    @Bean
    DirectExchange verifyFaliedDirect()
    {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueEnum.QUEUE_REGISTER_FAILED.getExchange())
                .durable(true).build();
    }
    //审核失败延迟队列绑定交换机
    @Bean
    DirectExchange verifyFailedTtlDirect()
    {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueEnum.QUEUE_REGISTER_FALIED_TTL.getExchange())
                .durable(true).build();
    }
    //审核失败实际队列
    @Bean
    public Queue verifyFailedQueue()
    {
        return new Queue(QueueEnum.QUEUE_REGISTER_FAILED.getName());
    }
    //审核失败延迟队列
    @Bean
    public Queue verifyFailedTtlQueue(){
        return QueueBuilder.durable(QueueEnum.QUEUE_REGISTER_FALIED_TTL.getName())
                .withArgument("x-dead-letter-exchange",QueueEnum.QUEUE_REGISTER_FAILED.getExchange())
                .withArgument("x-dead-letter-routing-key",QueueEnum.QUEUE_REGISTER_FAILED.getRouteKey())
                .build();
    }
    //审核核失败实际队列绑定到交换机
    @Bean
    Binding verifyFailedBinding(DirectExchange verifyFaliedDirect, Queue verifyFailedQueue)
    {
        return BindingBuilder.bind(verifyFailedQueue).to(verifyFaliedDirect).with(QueueEnum.QUEUE_REGISTER_FAILED.getRouteKey());
    }
    //审核失败延迟队列绑定到交换机
    @Bean
    Binding verifyFailedTtlBinding(DirectExchange verifyFailedTtlDirect,Queue verifyFailedTtlQueue)
    {
        return BindingBuilder.bind(verifyFailedTtlQueue).to(verifyFailedTtlDirect).with(QueueEnum.QUEUE_REGISTER_FALIED_TTL.getRouteKey());
    }
    //注册流程实际队列绑定交换机
    @Bean
    DirectExchange registerDirect()
    {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueEnum.QUEUE_REGISTER.getExchange())
                .durable(true).build();
    }
    //注册流程延迟队列绑定交换机
    @Bean
    DirectExchange registerTtlDirect()
    {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueEnum.QUEUE_REGISTER_TTL.getExchange())
                .durable(true).build();
    }
    //注册流程实际队列
    @Bean
    public Queue registerQueue()
    {
        return new Queue(QueueEnum.QUEUE_REGISTER.getName());
    }
    //注册流程延迟队列
    @Bean
    public Queue registerTtlQueue(){
        return QueueBuilder.durable(QueueEnum.QUEUE_REGISTER_TTL.getName())
                .withArgument("x-dead-letter-exchange",QueueEnum.QUEUE_REGISTER.getExchange())
                .withArgument("x-dead-letter-routing-key",QueueEnum.QUEUE_REGISTER.getRouteKey())
                .build();
    }
    //注册流程实际队列绑定到交换机
    @Bean
    Binding registerBinding(DirectExchange registerDirect, Queue registerQueue)
    {
        return BindingBuilder.bind(registerQueue).to(registerDirect).with(QueueEnum.QUEUE_REGISTER.getRouteKey());
    }
    //注册流程延迟队列绑定到交换机
    @Bean
    Binding registerTtlBinding(DirectExchange registerTtlDirect,Queue registerTtlQueue)
    {
        return BindingBuilder.bind(registerTtlQueue).to(registerTtlDirect).with(QueueEnum.QUEUE_REGISTER_TTL.getRouteKey());
    }
    //案件审核
    @Bean
    DirectExchange authCaseDirect()
    {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueEnum.QUEUE_AUTHCASE.getExchange())
                .durable(true).build();
    }

    @Bean
    DirectExchange authCaseTtlDirect()
    {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueEnum.QUEUE_AUTHCASE_TTL.getExchange())
                .durable(true).build();
    }

    @Bean
    public Queue authCaseQueue()
    {
        return new Queue(QueueEnum.QUEUE_AUTHCASE.getName());
    }

    @Bean
    public Queue authCaseTtlQueue(){
        return QueueBuilder.durable(QueueEnum.QUEUE_AUTHCASE_TTL.getName())
                .withArgument("x-dead-letter-exchange",QueueEnum.QUEUE_AUTHCASE.getExchange())
                .withArgument("x-dead-letter-routing-key",QueueEnum.QUEUE_AUTHCASE.getRouteKey())
                .build();
    }
    @Bean
    Binding authCaseBinding(DirectExchange authCaseDirect, Queue authCaseQueue)
    {
        return BindingBuilder.bind(authCaseQueue).to(authCaseDirect).with(QueueEnum.QUEUE_AUTHCASE.getRouteKey());
    }
    @Bean
    Binding authCaseTtlBinding(DirectExchange authCaseTtlDirect,Queue authCaseTtlQueue)
    {
        return BindingBuilder.bind(authCaseTtlQueue).to(authCaseTtlDirect).with(QueueEnum.QUEUE_AUTHCASE_TTL.getRouteKey());
    }
}
